//
//  ViewController.m
//  TraineeManagementSystem
//
//  Created by Dhanashree Bodas on 23/01/15.
//  Copyright (c) 2015 Dhanashree Bodas. All rights reserved.
//

#import "LoginViewController.h"
#import "TraineeViewController.h"
#import "CoursesViewController.h"
#import "AllocationViewController.h"
#import "AdminTable.h"
#import "Trainee.h"
#import "ListCourseViewController.h"
#import "LogoutViewController.h"
@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
        // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)usernameEntered:(id)sender {
    
    [self resignFirstResponder];
}


- (IBAction)passwordEntered:(id)sender {
    [self resignFirstResponder];
}


- (IBAction)loginPressed:(id)sender {
    
    AdminTable *ad=[[AdminTable alloc] init];
    TraineeTable *t=[[TraineeTable alloc] init];
    
    int aid=[ad Login:self.username.text andPassword:self.password.text];
       if(aid!=0)
    {
        [self performSegueWithIdentifier:@"adminSegue" sender:self];
        //[self dismissViewControllerAnimated:YES completion:nil];
    
    }
    else if(aid==0)
    {
        int tid=[t Login:self.username.text andPassword:self.password.text];
        
        if(tid!=0)
        {
            
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Trainee" bundle:nil];
            
            
           
            ListCourseViewController *vc = [sb instantiateViewControllerWithIdentifier:@"ListCourseViewController"];
          
              [vc setTraineeId:tid];
            UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:vc];
            
            
          
            
            NSLog(@"in login tid=%d",vc.TraineeId);
            
            [self presentViewController:nav animated:YES completion:nil];
            
        }
        else
        {
            
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Invalid login" message:@"Invalid username or password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
        }
    }
    else
    {
        
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Invalid login" message:@"Invalid username or password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }

    
   
}



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"adminSegue"]) {
        UITabBarController *tab=[segue destinationViewController];

        
        //For  tab 1
        TraineeViewController *trainee=[tab.viewControllers objectAtIndex:0];
        trainee.Title=@"Trainee";
        trainee.tabBarItem.image=[UIImage imageNamed:@"trainee.png"];
        
        
        //For  tab 2
        CoursesViewController *course=[tab.viewControllers objectAtIndex:1];
        course.Title=@"Courses";
        course.tabBarItem.image=[UIImage imageNamed:@"courses.png"];
        

        //For  tab 3
        AllocationViewController *a=[tab.viewControllers objectAtIndex:2];
        a.Title=@"Allocate";
        a.tabBarItem.image=[UIImage imageNamed:@"allocate1.png"];
        
        //for tab4
        LogoutViewController *lo=[tab.viewControllers objectAtIndex:3];
        lo.title=@"Logout";
        lo.tabBarItem.image=[UIImage imageNamed:@"logout.png"];
}
}
@end
